﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.Drawing.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;

namespace Financieras
{
    public partial class Cam : Form
    {
        public Cam()
        {
            InitializeComponent();
        }

        private FilterInfoCollection videoCaptura;
        private VideoCaptureDevice DispositivoDeVideo;
        public Image imagenFinal;

        private void Cam_Load(object sender, EventArgs e)
        {
            try
            {
                videoCaptura = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                foreach (FilterInfo videoCaptura__1 in videoCaptura)
                {
                    comboBoxCamara.Items.Add(videoCaptura__1.Name);
                }
                comboBoxCamara.SelectedIndex = 0;
                DispositivoDeVideo = new VideoCaptureDevice(videoCaptura[comboBoxCamara.SelectedIndex].MonikerString);
                DispositivoDeVideo.NewFrame += new NewFrameEventHandler(video_NuevoFrame);
                DispositivoDeVideo.Start();
            }
            catch (Exception ex)
            {

                MessageBox.Show("No cuenta con Camara", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void video_NuevoFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap Imagen = (Bitmap)eventArgs.Frame.Clone();

            pictureBoxImage.Image = Imagen;
        }

        private void btnCaptura_Click(object sender, EventArgs e)
        {
            if (DispositivoDeVideo.IsRunning == true)
            {
                DispositivoDeVideo.Stop();
            }
            imagenFinal = pictureBoxImage.Image;

            this.Close();

        }

        private void Cam_FormClosing(object sender, FormClosingEventArgs e)
        {
            DispositivoDeVideo.Stop();
        }
    }
}
